package com.usian.common.exception;



import com.baomidou.mybatisplus.extension.api.R;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.common.enums.AppHttpCodeEnum;
import lombok.extern.log4j.Log4j2;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@ControllerAdvice//控制器增强
@Log4j2
public class ExceptionCatch {


    //捕获Exception此类异常
    @ExceptionHandler(Exception.class)
    @ResponseBody
    public ResponseResult exception(Exception exception) {
        exception.printStackTrace();
        //记录日志
        log.error("catch exception:{}", exception.getMessage());

        if (exception instanceof  ArithmeticException){
            return ResponseResult.errorResult(AppHttpCodeEnum.ARITH_ERROR);
        }else if (exception instanceof NullPointerException){
            return ResponseResult.errorResult(AppHttpCodeEnum.NULL_ERROR);
        }

        //返回通用异常
        return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR);
    }
}

